/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
Table II.3
***************************************/
use "TSI_Imports",clear

drop if IPRM==1
keep if year==2011 | year==2012
gen Post=(year==2012)

* Keep products imported by the firm in both years
collapse (sum) dolar ,by(firm_ID hs6 country_code Post)
bys firm_ID hs6,sort: egen minPost=min(Post)
drop if min==1
bys firm_ID hs6,sort: egen maxPost=max(Post)
drop if max==0
drop min max

reshape wide dolar,i(firm_ID hs6 country_code) j(Post)
reshape long dolar,i(firm_ID hs6 country_code) j(Post)

*Identify source countries added in 2012
replace dolar=0 if dolar==.
sort firm_ID hs6 country_code Post
by firm_ID hs6 country_code: gen new=(dolar>0 & dolar[_n-1]==0)

keep if Post==1
collapse (sum) dolar ,by(firm_ID new )

reshape wide dolar,i(firm_ID ) j(new)
for X in any 0 1: replace dolarX=0 if dolarX==.

gen NewShr_f=dolar1/(dolar0+dolar1)
keep firm_ID NewShr_f
destring firm_ID,replace
sort firm_ID 
save tempnew.dta,replace

use tempall.dta,clear

keep if BartikExposure>0
keep if year==2012

sort firm_ID
merge firm_ID using tempnew.dta
drop _m

reghdfe  NewShr_f FirmExposure  lnEmp0 ImpShr0 ,absorb( sr ) vce(cluster sr)
estimates store col1
estadd local FE "sr" , replace
estadd local cluster "sr", replace

reghdfe  NewShr_f BartikExposure  lnEmp0 ImpShr0 ,absorb( sr ) vce(cluster sr)
estimates store col2
estadd local FE "sr" , replace
estadd local cluster "sr", replace

esttab col1 col2, compress width(1\hsize)  se  star(* 0.10 ** 0.05 *** 0.01) s(N r2 cluster   FE )

